package com.ruoyi.merchant.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.InnerAuth;
import javax.xml.crypto.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.ServiceOrder;
import com.ruoyi.merchant.service.IServiceOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/order")
public class ServiceOrderController extends BaseController
{
    @Autowired
    private IServiceOrderService serviceOrderService;



    /**
     * 查询订单列表
     */
    @RequiresPermissions("merchant:order:list")
    @GetMapping("/list")
    public TableDataInfo list(ServiceOrder serviceOrder)
    {
        startPage();
        List<ServiceOrder> list = serviceOrderService.selectServiceOrderList(serviceOrder);
        System.out.println();
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("merchant:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceOrder serviceOrder)
    {
        List<ServiceOrder> list = serviceOrderService.selectServiceOrderList(serviceOrder);
        ExcelUtil<ServiceOrder> util = new ExcelUtil<ServiceOrder>(ServiceOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
//    @RequiresPermissions("merchant:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ServiceOrder serviceOrder = serviceOrderService.selectServiceOrderById(id);
        return success(serviceOrderService.selectServiceOrderById(id));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("merchant:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceOrder serviceOrder)
    {
        return toAjax(serviceOrderService.insertServiceOrder(serviceOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("merchant:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceOrder serviceOrder)
    {
        return toAjax(serviceOrderService.updateServiceOrder(serviceOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("merchant:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serviceOrderService.deleteServiceOrderByIds(ids));
    }


    @InnerAuth
    @PutMapping("/orderTimeout/{id}")
    public int ordertimeout(@PathVariable("id") Long id) {
        return serviceOrderService.ordertimeout(id);
    }
    /**
     * 查询订单收入等等
     */
    @RequiresPermissions("merchant:order:list")
    @GetMapping("/count1")
    public AjaxResult count1(ServiceOrder serviceOrder)
    {
        Long mid = serviceOrder.getMid();
        Date useTime = serviceOrder.getUseTime();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String data = simpleDateFormat.format(useTime);
        System.out.println(mid);
        System.out.println(data);
        Map<String, Object> map = serviceOrderService.selectCount1(mid, data);
        return AjaxResult.success(map);
    }

    @RequiresPermissions("merchant:order:list")
    @GetMapping("/count2")
    public AjaxResult count2(ServiceOrder serviceOrder)
    {
        Long mid = serviceOrder.getMid();
        Date useTime = serviceOrder.getUseTime();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String data = simpleDateFormat.format(useTime);
        System.out.println("11111111111111111111111111");
        System.out.println(mid);
        System.out.println(data);
        Map<Long, Map<String, Object>>map = serviceOrderService.selectCount2(mid, data);
        return AjaxResult.success(map);
    }
}
